package com.sopra.tecnica.poc.server.util;

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class Signature {

	public static File createFile(File file, String path) {
		try {
			DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
			DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
			Document doc = docBuilder.newDocument();

			Element svg = doc.createElementNS("http://www.w3.org/2000/svg", "svg");
			svg.setAttribute("version", "1.1");
			svg.setAttribute("width", "100%");
			svg.setAttribute("height", "100%");
			svg.setAttribute("viewBox", "0 0 1500 1500");
			svg.setAttribute("preserveAspectRatio", "xMidYMid meet");
			doc.appendChild(svg);

			Element pathElement = doc.createElement("path");
			pathElement.setAttribute("stroke", "black");
			pathElement.setAttribute("stroke-width", "7");
			pathElement.setAttribute("fill", "none");
			pathElement.setAttribute("d", path);
			svg.appendChild(pathElement);

			// set up a transformer
			TransformerFactory transfac = TransformerFactory.newInstance();
			Transformer trans = transfac.newTransformer();
			trans.setOutputProperty(OutputKeys.INDENT, "yes");

			DOMSource source = new DOMSource(doc);
			
//			StreamResult result = new StreamResult(System.out);
//			trans.transform(source, result);
//			System.out.println(file.getAbsolutePath());
			trans.transform(source, new StreamResult(file));
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return file;
	}
}
